package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    //菜品列表分頁查詢
    @GetMapping("/dish/page")
    public ResultInfo FindByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String name
    ){
    //1.调用service查询
        Page<Dish> page = dishService.findByPage(name,pageNum,pageSize);

        return ResultInfo.success(page);
    }

    //保存新增菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish){
         dishService.save(dish);
         return ResultInfo.success(null);
    }

    //根据主键查询
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id){
        Dish dish = dishService.findById(id);
        return ResultInfo.success(dish);
    }

    //修改
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish){
        dishService.update(dish);
        return ResultInfo.success(null);
    }
    //dish/status/1?ids=1397849739276890114,13978501409821
    //批量启用停售
    @PostMapping("/dish/status/{status}")
    public ResultInfo startOrEnd(@PathVariable("status") Integer status ,Long[] ids){
        if (ids!=null&&ids.length>0){
            dishService.startOrEnd(ids,status);
        }
        return ResultInfo.success(null);
    }
    //批量删除
    @DeleteMapping("/dish")
    public ResultInfo deleteByIds(Long[] ids){
        if (ids!=null&&ids.length>0){
            dishService.deleteByIds(ids);
        }
        return ResultInfo.success(null);
    }
    //回显菜单
    @GetMapping("/dish/list")
    public ResultInfo findList(Long categoryId,@RequestParam(defaultValue = "1") Integer status,String name){
        List<Dish> dishList = dishService.findList(categoryId,status,name);
        return ResultInfo.success(dishList);
    }
}
